﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Sovigaz.DataProvider;
using Sovigaz.Permission.Repositories;
using Utilities;

namespace Sovigaz.Permission.Api
{
    public class TestApiController : ApiController
    {
        public TestRepository Get(int? pageNumber, string orderBy, string orderDirection, string testCode, string productID, string productCode, string ownerID, string dateFrom, string dateTo)
        {
            try
            {
                if (dateFrom.Contains("NaN")) dateFrom = string.Empty;
                if (dateTo.Contains("NaN")) dateTo = string.Empty;
                string whereClause = "IsDeleted != true";
                if (!String.IsNullOrWhiteSpace(testCode))
                    whereClause = String.Format(whereClause + " AND TestCode.Contains(\"{0}\")", testCode);
                if (!String.IsNullOrWhiteSpace(productCode))
                    whereClause = String.Format(whereClause + " AND ProductCode.Contains(\"{0}\")", productCode);
                if (!String.IsNullOrWhiteSpace(ownerID))
                    whereClause = String.Format(whereClause + " AND OwnerID = {0}", ownerID);
                if (!String.IsNullOrWhiteSpace(productID))
                    whereClause = String.Format(whereClause + " AND productID = {0}", productID);
                if (!String.IsNullOrWhiteSpace(dateFrom.ToString()) && !String.IsNullOrWhiteSpace(dateTo.ToString()))
                    whereClause = String.Format(whereClause + " AND TestedDate >= \"{0}\" AND TestedDate <= \"{1}\"", dateFrom, dateTo);

                return new TestRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public Test Get(int? id)
        {
            try
            {
                Test test = new Test();
                var repository = new TestRepository();
                test.TestCode = GenerateCode.GetCode(repository.GetNextIndex(), CodeType.Test);
                if (id.HasValue && id > 0)
                    test = repository.GetByID(id.Value);
                return test;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public void Delete(int? id)
        {
            try
            {
                if (id.HasValue && id > 0)
                {
                    var repository = new TestRepository();
                    repository.Delete(id.Value);
                }
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public TestRepository Delete(string ids, int? pageNumber, string orderBy, string orderDirection, string testCode, string productID, string productCode, string ownerID, string dateFrom, string dateTo)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');

                var repository = new TestRepository();
                repository.Delete(idArr);

                return Get(pageNumber, orderBy, orderDirection, testCode,productID, productCode, ownerID, dateFrom, dateTo);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public Test Put(Test test)
        {
            try
            {
                var repository = new TestRepository();
                int id = repository.Update(test);

                return Get(id);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
            }
        }

        public List<Test> Get()
        {
            try
            {
                return new TestRepository().GetListTest();
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }
    }
}
